package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userCategoryController")
@RequestMapping("/user/category")
@Slf4j
@Tag(name = "C端分类管理接口")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类条件查询
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "分类条件查询")
    public Result<List<Category>> list(@RequestParam(required = false) Integer type) {
        log.info("分类条件id{}", type);
        List<Category> categoryList = categoryService.list(type);
        if (CollectionUtils.isNotEmpty(categoryList)) {
            return Result.success(categoryList);
        } else {
            // 此分类下没有菜品或套餐
            return Result.error(MessageConstant.CATEGORY_UNDER_NODISHES);
        }
    }
}
